/*
 * Copyright (c) 2008-2009, Petr Panteleyev
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 * 
 *    1. Redistributions of source code must retain the above copyright notice, 
 *       this list of conditions and the following disclaimer.
 *    2. Redistributions in binary form must reproduce the above copyright notice, 
 *       this list of conditions and the following disclaimer in the documentation 
 *       and/or other materials provided with the distribution.
 *    3. The name of the author may not be used to endorse or promote products 
 *       derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR 
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN 
 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package org.panteleyev.blogclient.profwizard;

import java.awt.Dimension;

import javax.swing.Box;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import org.panteleyev.blogclient.ProfileDialog;

/**
 *
 * @author Petr Panteleyev
 */
final class UserCredentialsPanel extends JPanel implements IWizardPage {

    /** Creates new form ProfileTypeSelectionPanel */
    public UserCredentialsPanel() {
        add(Box.createVerticalGlue());
        initComponents();
        add(Box.createVerticalGlue());
        
        Dimension dMax = textPanel.getMaximumSize();
        dMax.height = textPanel.getMinimumSize().height;               
        textPanel.setMaximumSize(dMax);
        
        dMax = userDataPanel.getMaximumSize();
        dMax.height = userDataPanel.getMinimumSize().height;               
        userDataPanel.setMaximumSize(dMax);
        
        
    }
    
    String getUserName() { return userNameEdit.getText(); }
    String getPassword() { return new String(passwordEdit.getPassword()); }
    boolean getStorePassword() { return storePasswordCheckBox.isSelected(); }
    
    @Override
    public boolean isNextAllowed() {
        if (getUserName().length() == 0) {
            JOptionPane.showMessageDialog(this, "User name cannot be empty");
            return false;
        } else {
            return true;
        }
    }    

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        blogTypeButtonGroup = new javax.swing.ButtonGroup();
        textPanel = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        userDataPanel = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        userNameEdit = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        storePasswordCheckBox = new javax.swing.JCheckBox();
        passwordEdit = new javax.swing.JPasswordField();

        setLayout(new javax.swing.BoxLayout(this, javax.swing.BoxLayout.Y_AXIS));

        textPanel.setAlignmentX(0.1F);
        textPanel.setLayout(new java.awt.BorderLayout(10, 0));

        jLabel1.setText(L10N.USER_DATA_PANEL_TEXT.s());
        textPanel.add(jLabel1, java.awt.BorderLayout.CENTER);

        add(textPanel);

        userDataPanel.setAlignmentX(0.0F);
        userDataPanel.setLayout(new java.awt.GridBagLayout());

        jLabel2.setText(ProfileDialog.USER_NAME_LABEL);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 0);
        userDataPanel.add(jLabel2, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 3, 0, 5);
        userDataPanel.add(userNameEdit, gridBagConstraints);

        jLabel3.setText(ProfileDialog.PASSWORD_LABEL);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(3, 5, 0, 0);
        userDataPanel.add(jLabel3, gridBagConstraints);

        storePasswordCheckBox.setText(ProfileDialog.REMEMBER_PASSWORD_CHECKBOX);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(3, 3, 5, 5);
        userDataPanel.add(storePasswordCheckBox, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(3, 3, 0, 5);
        userDataPanel.add(passwordEdit, gridBagConstraints);

        add(userDataPanel);
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup blogTypeButtonGroup;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPasswordField passwordEdit;
    private javax.swing.JCheckBox storePasswordCheckBox;
    private javax.swing.JPanel textPanel;
    private javax.swing.JPanel userDataPanel;
    private javax.swing.JTextField userNameEdit;
    // End of variables declaration//GEN-END:variables

    @Override
    public String getPrevPage() {
        return NewProfileWizard.ACCOUNT_TYPE_PAGE;
    }

    @Override
    public String getNextPage() {
        return NewProfileWizard.PROFILE_OPTIONS_PAGE;
    }

}
